/*
 * File:   window_form.cpp
 * Author: dima
 *
 * Created on 19 октября 2014 г., 0:31
 */
#include <QFileDialog>
#include <QMessageBox>
#include "form_scaling.h"
#include "window_form.h"
#include "ui_window_form.h"
#include "ipd_img.h"
#include "ipd_png.h"
#include "ipd_exception.h"

window_form::window_form() : widget(new Ui::window_form())
{
    widget->setupUi(this);
    img = new ipd::ipd_img();
    frm_scaling = NULL;
}

window_form::~window_form()
{
    delete_form_scaling();
    delete img;
    delete widget;
}

void window_form::convert_image()
{
    if (create_form_scaling())
        frm_scaling->show();
}

void window_form::save_image()
{
    QString file_path = QFileDialog::getSaveFileName(0, "Save Image", "", "*.png");
    if (file_path.isEmpty())
    {
        QMessageBox::warning(0, QString::fromUtf8("Warning"), QString::fromUtf8("The file is not selected"), 0, 0);
        return;
    }
    ipd::ipd_png *png = new ipd::ipd_png();
    try
    {
        if (frm_scaling == NULL)
            throw ipd::ipd_exception(std::string("Class \"form_scaling\" is not created"), std::string("main_form"));
        png->save_png(*frm_scaling->get_convertible_image(), file_path.toStdString());
    }
    catch (ipd::ipd_exception &except)
    {
        QMessageBox::critical(0, QString::fromUtf8(except.in_file().c_str()), QString::fromUtf8(except.what().c_str()), 0, 0);
    }
    delete png;

}

void window_form::open_image()
{
    QString file_path = QFileDialog::getOpenFileName(0, "Open Image", "", "*.png");
    if (file_path.isEmpty())
        return;
    delete_form_scaling();
    ipd::ipd_png *png = new ipd::ipd_png();
    try
    {
        png->load_png(*img, file_path.toStdString());
        show_img(widget->original_image, *img);
    }
    catch (ipd::ipd_exception &except)
    {
        QMessageBox::critical(0, QString::fromUtf8(except.in_file().c_str()), QString::fromUtf8(except.what().c_str()), 0, 0);
    }
    delete png;
}

void window_form::get_image(ipd::ipd_img& image)
{
    show_img(widget->transformed_image, image);
}

void window_form::show_img(QLabel *label, ipd::ipd_img &image)
{
    int image_width = image.get_width();
    int image_height = image.get_height();
    unsigned char *data = image.get_data();

    //QImage::Format_RGB888
    //QImage::Format_Indexed8
    QImage::Format format;
    switch (image.get_color_type())
    {
    case ipd::tp_RGB:
    {
        format = QImage::Format_RGB888;
        break;
    }
    case ipd::tp_mono:
    {
        format = QImage::Format_Indexed8;
        break;
    }
    default:
    {
        throw ipd::ipd_exception(std::string("Image model is not supported (Supports RGB and monochrome image)"), std::string("main_form"));
    }
    }
    QPixmap pixmap = QPixmap::fromImage(
                                        QImage(
                                               (unsigned char *) data,
                                               image_width,
                                               image_height,
                                               format
                                               )
                                        );
    label->setPixmap(pixmap);
}

void window_form::delete_form_scaling()
{
    if (frm_scaling != NULL)
        delete frm_scaling;
    frm_scaling = NULL;
}

bool window_form::create_form_scaling()//create class form_spatial_smoothing_filters
{
    if (img == NULL)
    {
        QMessageBox::critical(0, QString::fromUtf8("main_form"), QString::fromUtf8("Image file is not loaded"), 0, 0);
        return false;
    }
    if (frm_scaling == NULL)
    {
        frm_scaling = new form_scaling(*img);
        connect(frm_scaling, SIGNAL(send_image(ipd::ipd_img &)), this, SLOT(get_image(ipd::ipd_img &)));
    }
    return true;
}
